﻿#include "TurnTableCodec.h"

#include "TcpCodec.h"

#include "TurnTableDecodeBase.h"
#include "TurnTableDecode_TurnTable_Connect.h"
#include "TurnTableDecode_TurnTable_Disconnect.h"
#include "TurnTableDecode_Thermostat_CurrentTemperature.h"
#include "TurnTableDecode_TurnTable_CurrentState.h"
#include "TurnTableDecode_TurnTable_SearchZeroState.h"
#include "TurnTableDecode_TurnTable_RunningState.h"
#include "TurnTableDecode_TurnTable_RunningMode.h"
#include "TurnTableDecode_TurnTable_Error.h"

TurnTableCodec::TurnTableCodec()//构造函数
{
  turnTableDecodeMap.insert(TurnTable_Connect,new TurnTableDecode_TurnTable_Connect(this));
  turnTableDecodeMap.insert(TurnTable_Disconnect,new TurnTableDecode_TurnTable_Disconnect(this));
  turnTableDecodeMap.insert(Thermostat_CurrentTemperature,new TurnTableDecode_Thermostat_CurrentTemperature(this));
  turnTableDecodeMap.insert(TurnTable_CurrentState,new TurnTableDecode_TurnTable_CurrentState(this));
  turnTableDecodeMap.insert(TurnTable_SearchZeroState,new TurnTableDecode_TurnTable_SearchZeroState(this));
  turnTableDecodeMap.insert(TurnTable_RunningState,new TurnTableDecode_TurnTable_RunningState(this));
  turnTableDecodeMap.insert(TurnTable_RunningMode,new TurnTableDecode_TurnTable_RunningMode(this));
  turnTableDecodeMap.insert(TurnTable_Error,new TurnTableDecode_TurnTable_Error(this));
}

TurnTableCodec::~TurnTableCodec()
{
  for(int key:turnTableDecodeMap.keys())
    {
      delete turnTableDecodeMap.take(key);
    }
}

void TurnTableCodec::sendInstruction(uint deviceId, TurnTableInstructionFlag instructionFlag, float arg1, float arg2, float arg3)
{
  tcpCodec->code(TcpCodec::TurnTable,
                 QByteArray().append((char*)&deviceId,4)
                             .append(instructionFlag)
                             .append((char*)&arg1,4)
                             .append((char*)&arg2,4)
                             .append((char*)&arg3,4));
}

uchar TurnTableCodec::getType(QByteArray data)
{
  return (uchar)data.at(0);
}

void TurnTableCodec::decode(QByteArray instruction)
{
  char* dataPtr=instruction.data();
  uchar type=dataPtr[0];
  if(turnTableDecodeMap.contains(type))
    {
      turnTableDecodeMap.value(type)->decode(dataPtr+1);
    }
}

float TurnTableCodec::getTurnTablePositionUpperLimit()
{
  return turnTablePositionUpperLimit;
}
TurnTableCodec* TurnTableCodec::setTurnTablePositionUpperLimit(float turnTablePositionUpperLimit)
{
  this->turnTablePositionUpperLimit=turnTablePositionUpperLimit;
  return this;
}
float TurnTableCodec::getTurnTablePositionLowerLimit()
{
  return turnTablePositionLowerLimit;
}
TurnTableCodec* TurnTableCodec::setTurnTablePositionLowerLimit(float turnTablePositionLowerLimit)
{
  this->turnTablePositionLowerLimit = turnTablePositionLowerLimit;
  return this;
}
float TurnTableCodec::getTurnTableRateUpperLimit()
{
  return turnTableRateUpperLimit;
}
TurnTableCodec* TurnTableCodec::setTurnTableRateUpperLimit(float turnTableRateUpperLimit)
{
  this->turnTableRateUpperLimit = turnTableRateUpperLimit;
  return this;
}
float TurnTableCodec::getTurnTableRateLowerLimit()
{
  return turnTableRateLowerLimit;
}
TurnTableCodec* TurnTableCodec::setTurnTableRateLowerLimit(float turnTableRateLowerLimit)
{
  this->turnTableRateLowerLimit = turnTableRateLowerLimit;
  return this;
}

float TurnTableCodec::getTurnTableAccelerationUpperLimit()
{
  return turnTableAccelerationUpperLimit;
}

TurnTableCodec* TurnTableCodec::setTurnTableAccelerationUpperLimit(float turnTableAccelerationUpperLimit)
{
  this->turnTableAccelerationUpperLimit = turnTableAccelerationUpperLimit;
  return this;
}

float TurnTableCodec::getTurnTableAccelerationLowerLimit()
{
  return turnTableAccelerationLowerLimit;
}

TurnTableCodec *TurnTableCodec::setTurnTableAccelerationLowerLimit(float turnTableAccelerationLowerLimit)
{
  this->turnTableAccelerationLowerLimit = turnTableAccelerationLowerLimit;
  return this;
}

TcpCodec* TurnTableCodec::getTcpCodec()const
{
  return tcpCodec;
}

TurnTableCodec* TurnTableCodec::setTcpCodec(TcpCodec*const tcpCodec)
{
  this->tcpCodec=tcpCodec;
  printInjection(this,tcpCodec);
  return this;
}

void TurnTableCodec::objectAutoInjection(QHash<QString,QObject*>* objMap)
{
  setTcpCodec(getObjFromMap<TcpCodec>(objMap));
}
